package com.sc.nft.dao;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.nft.dto.AdminShareProfitCollectDetailsDTO;
import com.sc.nft.dto.AdminShareProfitCollectDetailsPointsDTO;
import com.sc.nft.entity.EcologyHatchShareProfit;
import com.sc.nft.entity.MerchantPledgeTaskConf;
import com.sc.nft.entity.NewEcologyHatchShareProfit;
import com.sc.nft.enums.EcologyHatchShareProfitTypeEnum;
import com.sc.nft.enums.EcologyCollectionShareProfitTypeEnum;
import com.sc.nft.enums.NewEcologyHatchShareProfitStatus;
import com.sc.nft.mapper.NewEcologyHatchShareProfitMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;

import java.util.Date;
import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

/**
 * @Author：LiGen
 * @Package：com.sc.nft.dao
 * @Project：sc_metago
 * @name：DaoEcologyPointsWalletDao
 * @Date：2024/11/26 11:08
 * @Filename：DaoEcologyPointsWalletDao
 */
@Slf4j
@Repository
public class NewEcologyHatchShareProfitDao extends ServiceImpl<NewEcologyHatchShareProfitMapper, NewEcologyHatchShareProfit> {






    /**
     * 获取熔炼记录的分润记录
     *
     * @param userRecordId
     * @return
     */
    public List<NewEcologyHatchShareProfit> getByUserRecordId(Long userRecordId, EcologyCollectionShareProfitTypeEnum type) {
        LambdaQueryWrapper<NewEcologyHatchShareProfit> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(NewEcologyHatchShareProfit::getUserRecordId, userRecordId);
        if (Objects.nonNull(type)) {
            wrapper.eq(NewEcologyHatchShareProfit::getType, type);
        }
        return list(wrapper);
    }

    /**
     * 获取上周分红池
     * @param daoId
     * @param type
     * @return
     */
    public BigDecimal getLastWeekBonusPool(Long daoId, EcologyCollectionShareProfitTypeEnum type,Long hatchCollectionId) {
        DateTime beginOfWeek = DateUtil.beginOfWeek(DateUtil.lastWeek());
        DateTime endOfWeek = DateUtil.endOfWeek(DateUtil.lastWeek());
        return baseMapper.getLastWeekBonusPool(daoId,type.getCode(),beginOfWeek,endOfWeek,hatchCollectionId);
    }

    public Boolean updateSettleStatusById(Long id, NewEcologyHatchShareProfitStatus newEcologyHatchShareProfitStatus,Long shareProfitDaoPoolId) {
        LambdaUpdateWrapper<NewEcologyHatchShareProfit> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(NewEcologyHatchShareProfit::getId, id);
        wrapper.set(NewEcologyHatchShareProfit::getSettleStatus, newEcologyHatchShareProfitStatus);
        if (Objects.nonNull(shareProfitDaoPoolId)){
            wrapper.set(NewEcologyHatchShareProfit::getShareProfitDaoPoolId, shareProfitDaoPoolId);
        }
        return update(new NewEcologyHatchShareProfit(), wrapper);
    }


    /**
     * 获取不同类型分润下不同状态的分润记录
     *
     * @param type
     * @param settleStatus
     * @return
     */
    public List<NewEcologyHatchShareProfit> getListByTypeAndStarts(EcologyCollectionShareProfitTypeEnum type, NewEcologyHatchShareProfitStatus settleStatus) {
        LambdaQueryWrapper<NewEcologyHatchShareProfit> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(NewEcologyHatchShareProfit::getType, type);
        wrapper.eq(NewEcologyHatchShareProfit::getSettleStatus, settleStatus);
        return list(wrapper);
    }
    public AdminShareProfitCollectDetailsPointsDTO getNewSumDistributionPointsGroupTypeByCollectionId(Long collectionId) {
        return baseMapper.getNewSumDistributionPointsGroupTypeByCollectionId(collectionId);
    }


}
